package org.jwflow.design.servlet;

import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.jwflow.design.base.BaseServlet;
import org.jwflow.design.utils.JwFlowPlugin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/wf_design")
public class WfDesignServlet extends BaseServlet {

    public void index(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String id=request.getParameter("id")==null?"":request.getParameter("id");
        //装载map数据模型信息
        Map root = new HashMap();
        root.put("id", id);

        Template template=cfg.getTemplate("wf_design.html","UTF-8");
        //设置响应输出类型和编码格式
        //设置浏览器用utf8来解析返回的数据
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        //设置servlet用UTF-8转码，而不是用默认的ISO8859
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();//创建相应内容写出对象

        //将map数据模型与模板结合输出
        try {
            template.process(root, out);
        } catch (TemplateException e) {
            throw new ServletException("加载Freemarker模板失败！",e);
        }

    }

    public void prop(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //节点ID
        String node_id=request.getParameter("node_id");
        //工作流ID
        String wf_id=request.getParameter("wf_id");

        //装载map数据模型信息
        Map root = new HashMap();
        root.put("node_id", node_id);
        root.put("wf_id", wf_id);

        /*WfCodeModel wfCodeModel= codeService.getWfCode(wf_id);
        set("wf",wfCodeModel);

        WfNodeModel wfNodeModel=nodeService.getNode(node_id);
        set("node",wfNodeModel);*/

        Template template=cfg.getTemplate("wf_design_prop.html","UTF-8");
        //设置响应输出类型和编码格式
        //设置浏览器用utf8来解析返回的数据
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        //设置servlet用UTF-8转码，而不是用默认的ISO8859
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();//创建相应内容写出对象

        //将map数据模型与模板结合输出
        try {
            template.process(root, out);
        } catch (TemplateException e) {
            throw new ServletException("加载Freemarker模板失败！",e);
        }

    }

    public void getJson()
    {
        JwFlowPlugin jwFlowPlugin=new JwFlowPlugin();
        jwFlowPlugin.start();

    }
}
